/**
 * Created by meiguizhinian on 2018/6/26
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

Object.size = function (obj) {
	let len = 0;
	for (let item in obj) {
		++len;
	}
	return len;
};

if (!Object.clone) {
	Object.clone = function(obj) {
		if (typeof obj === 'undefined') {
			return obj;
		} else if (typeof obj === "boolean") {
			return obj;
		} else if (typeof obj === "number" || Object.prototype.toString.call(obj) === '[object Number]') {
			return obj;
		} else if (typeof obj === "string" || Object.prototype.toString.call(obj) === '[object String]') {
			return obj;
		} else if (typeof obj ===  'function') {
			return obj;
		} else if (Array.isArray(obj) || (typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Array]')) {
			let array = [];
			for (let i=0; i!==obj.length; ++i) {
				array[i] = Object.clone(obj[i]);
			}
			return array;
		} else if (typeof obj === "object" && Object.prototype.toString.call(obj) === '[object Object]') {
			let object = {};
			for (let k in obj) {
				//if (obj.hasOwnProperty(i)) {}
				object[k] = Object.clone(obj[k]);
			}
			return object;
		} else {
			cc.log("Object.clone 未支持的类型");
			return null;
		}
	};
}

Array.prototype.back = function() {
	if (this.length===0) {
		return null;
	}
	return this[this.length-1];
};

if (!String.prototype.padStart) {
	String.prototype.padStart = function (maxLength, fillString=" ") {
		if (this.length>=maxLength) {
			return ""+this;
		} else {
			return fillString.repeat(maxLength-this.length)+this;
		}
	}
}
if (!String.prototype.padEnd) {
	String.prototype.padEnd = function (maxLength, fillString=" ") {
		if (this.length>=maxLength) {
			return this+"'";
		} else {
			return this+fillString.repeat(maxLength-this.length);
		}
	}
}
if (!String.prototype.toByteArray) {
	String.prototype.toByteArray = function() {
		let bytes = new Uint8Array(this.length);
		for (let i = 0; i !== this.length; i++) {
			bytes[i] = this.charCodeAt(i) & 0xff;
		}
		return bytes;
	};
} else {
	cc.log("toByteArray has defined");
}

if (cc.sys.isNative) {
	//修正代码实现不同的问题f
	cc.size = function (w, h) {
		// This can actually make use of "hidden classes" in JITs and thus decrease
		// memory usage and overall performance drastically
		//return cc.size(w, h);
		// but this one will instead flood the heap with newly allocated hash maps
		// giving little room for optimization by the JIT
		// note: we have tested this item on Chrome and firefox, it is faster than cc.size(w, h)
		if (w === undefined)
			return {width: 0, height: 0};
		if (h === undefined)
			return {width: w.width, height: w.height};
		return {width: w, height: h};
	};
} else {
	cc.rect = function(x,y,w,h) {
		let argLen = arguments.length;
		if (argLen === 0)
			return { x: 0, y: 0, width: 0, height: 0 };

		if (argLen === 1)
			return { x: x.x, y: x.y, width: x.width, height: x.height };

		if (argLen === 2)
			return { x: x.x, y: x.y, width: y.width, height: y.height };

		if (argLen === 4)
			return { x: x, y: y, width: w, height: h };

		throw "unknown argument type";
	};
}

if (!sp.Skeleton.prototype.getSkeleton) {
	sp.Skeleton.prototype.getSkeleton = function () {
		return this._skeleton;
	};
}

cc.GlyphCollection = {
	DYNAMIC : 0,
	NEHE    : 1,
	ASCII   : 2,
	CUSTOM  : 3
};